<?php
require_once('simple_html_dom.php');
class JWCInfoManager extends CI_Model {

    public function __construct()
    {
        $this->load->database();
		$this->load->model('Util');
		$this->load->model('RongImManager');
		$this->load->model('UserManager');
		$this->load->model('DatabaseManager');
		$this->load->model('SessionManager');
    }
	//  获取lt值  
	private function get_lt()
	{
		$html = new simple_html_dom();
		$html->load_file('http://cas.hdu.edu.cn/cas/login');
		$divs = $html->find('input[name=lt]');
		$lt=$divs[0]->attr;
		$html->clear();
		return $lt['value'];
	}
	
	//	用户登录
	public function login($username,$password){
			$row=$this->DatabaseManager->get_user_info($username);
			//	判断用戶是否已经注册		
			if($row==null){	
				$d=$this->get_user_from_jwc($username,$password);
				//	检验从教务处获取数据，为null表示教务处密码错误	
				if($d==null){
					return	$this->Util->format_return(0,0,'wrong password');
				}
				return	$this->Util->format_return(0,1,'no user',$d);
			}else{
				//	检查密码是否正确		
				if($password==$row['password']){
					$data=$this->UserManager->set_user_callback($row);
					return	$this->Util->format_return(1,1,'success',$data);
				}else{
					return	$this->Util->format_return(0,5,'wrong password');	
				}	
			}
	}
	public function register($username,$password,$name,$phone,$idcard,$sex){
		$this->DatabaseManager->save_user($username,$password,$name,$phone,$idcard,$sex);
		$data=$this->RongImManager->get_token_by_username($username);
		$data=json_decode($data);
		$data=array('username'=>$username,'name'=>$name,'integral'=>10,'sessionId'=>$this->SessionManager->set($username,$password),'sex'=>$sex,'token'=>($data->token));
		return	$this->Util->format_return(1,1,'create user',$data);
	}
	// 检查教务密码是否正确	
	public function get_user_from_jwc($username,$password){
		//请求参数		
		$getData='?lt='.$this->get_lt().'&encodedService=http%3a%2f%2fjxgl.hdu.edu.cn%2findex.aspx'.'&service=http://jxgl.hdu.edu.cn/index.aspx'
		.'&serviceName=null'.'&loginErrCnt=0'.'&username='.$username.'&password='.$password;
		//请求网址		
		$url='http://cas.hdu.edu.cn/cas/login';
		$html = new simple_html_dom();
		$html->load_file($url.$getData);
		$ret = $html->find('a',0)->href;
		$title=$html->title;
		//判断密码是否正确		
		if(strpos($html, 'http://jxgl.hdu.edu.cn/index.aspx?ticket=')!=0){
			$html->clear();
			return substr($ret,strlen('http://jxgl.hdu.edu.cn/index.aspx?ticket='));
		}
		return null;
		//print_r($ret);
	}
	// 获取教务处身份证信息 username(学号) name(姓名） idcard（身份证） cookie	
	public function get_user_info_from_jwc($username,$name,$idcard,$cookie){
		$url='http://jxgl.hdu.edu.cn/xs_main.aspx?xh='.$username;
		//	send_post(网址，ciikie值)为post请求方法	
		$html=$this->send_post($url,$cookie);
		//判断姓名或cookie是否正确		
		if(strpos($html, $name)!=0){
			//	姓名urlencode处理		
			$xm=urlencode(mb_convert_encoding($name,'gb2312','utf-8' ));
			$url=substr($html,strpos($html, 'xsgrxx.aspx'));
			$url=substr($url,0,strpos($url, '" tar')-1);
			//	获取	gnmkdm值
			$gnmkdm=substr($url,strpos($url, 'gnmkdm=')+7);
			//	教务处个人信息地址		
			$url='http://jxgl.hdu.edu.cn/xsgrxx.aspx?xh='.$username.'&xm='.$xm.'&gnmkdm='.$gnmkdm;
			$html=$this->send_post($url,$cookie);
			//	判断身份证信息是否正确		
			if(strlen($idcard)==18&&strpos($html, $idcard)!=0){
				//		判断性别		
				if(strpos($html, '男')!=0){
					$sex=0;	
				}else{
					$sex=1;	
				}
				return $this->Util->format_return(1,1,'success',$sex);
			}else{
				return $this->Util->format_return(0,3,'wrong idcard');
			}
		}else{
			if(strlen($html)<200){
				return	$this->Util->format_return(0,4,'wrong cookie');	
			}
			return	$this->Util->format_return(0,2,'wrong name');	
		}
	}
	//post请求	
	private function send_post($url, $cookie) {
 		//$postdata = http_build_query(array());  
		$options = array(  
			'http' => array(  
			'method' => 'GET',  
			'header' => array('Cookie:'.$cookie,'Referer:'.$url),
			//'content' => $postdata,  
			'timeout' => 15 * 60 // 超时时间（单位:s）  
    		)  
  		);  
		$context = stream_context_create($options);  
		$result = file_get_contents($url, false, $context);  
		$result =iconv("gb2312", "utf-8//IGNORE",$result);
		return $result;  
	} 
}